/*
 * TimersPool.java
 *
 * Created on 26 de marzo de 2006, 05:48 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package inc.jmc.cyberboard.ui.print;


import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.Attribute;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;
import javax.print.event.PrintServiceAttributeEvent;
import javax.print.event.PrintServiceAttributeListener;

import org.apache.log4j.Logger;

/**
 *
 * @author Juan Miguel Olguin Salguero
 */
public class PrintListenerUI implements PrintServiceAttributeListener, PrintJobListener{
    static Logger logger = Logger.getLogger(PrintListenerUI.class.getName());
    
    private PrintJobsInboxFrm frm;
    /**
	 * Descubre el servicio de impresion por default
	 * @return el objeto del servicio actual de impresion en esta maquina.
	 */
	public PrintService getDefaultPrintService() {
		PrintService defservices = PrintServiceLookup.lookupDefaultPrintService();
		logger.info("Impresora por default-->" + defservices);
		return defservices;
	}
    /**
     * @param frm de tipo PrintJobsInboxFrm, para saber donde colocar las llamadas a impresion que vayan llegando. (una JList o un JTable)
     */
    public PrintListenerUI(PrintJobsInboxFrm frm)
    {
    	this.frm = frm;	
    }
    /* (non-Javadoc)
     * @see javax.print.event.PrintServiceAttributeListener#attributeUpdate(javax.print.event.PrintServiceAttributeEvent)
     */
    public void attributeUpdate(PrintServiceAttributeEvent e){

    	logger.info(">>>>>>>>Print listener");
    	logger.info("Servicio de impresion-->" + e.getPrintService());
    	PrintServiceAttributeSet psas = e.getAttributes();
        Attribute[] nattr = psas.toArray();
        logger.info("numero de atributos-->" + nattr.length);
        for (int i = 0; i < nattr.length; i++) {
        	logger.info("Impresora-->" + nattr[i]);
        	logger.info("Clase-->" + nattr[i].getClass());
        	logger.info("Categoria-->" + nattr[i].getCategory());
        	logger.info("Nombre-->" + nattr[i].getName());
        }
        logger.info(">>>>>>>>Print listener ok");
    }
    /**
     * @return la lista de servicios, en objetos PrintService 
     */
    public PrintService[] getPrintServices() {
    	PrintService[] services;
    	services = PrintServiceLookup.lookupPrintServices(null, null);
    	return services;
    }
    
    static PrintJobEvent spje = null;
	static int setype = 0;

	public void printDataTransferCompleted(PrintJobEvent pje) {
		int etype = pje.getPrintEventType();
		logger.info("Data xfer completed !"); 
		this.setype = etype;
		try {
			Thread.sleep(100);
		} catch(Exception e) {
		logger.info(e.getMessage());
		}
	}

	public void printJobCompleted(PrintJobEvent pje) {
		logger.info("print Job completed successfully" + pje);
		this.spje = pje;
	}

	public void printJobFailed(PrintJobEvent pje) {
		logger.info("print job failed" + pje);
		this.spje = pje;
	}
	
	public void printJobNoMoreEvents(PrintJobEvent pje) {
		logger.info("All events DONE");
		this.spje = pje;
	}

	public void printJobRequiresAttention(PrintJobEvent pje) {
		logger.info("Print job requires attention " + pje);
		this.spje = pje;
	}

	public void printJobCanceled(PrintJobEvent pje) {
		logger.info("Print job cancelled ");
	}
	
	public PrintJobEvent getSpje() {
//		logger.info("Inside PJL spje=", + spje);
		return spje;
	}

	public int getSetype() {
		String str = Integer.toString(setype);
		logger.info("Inside PJL setype:");
		logger.info(str);
		return setype;
	}
    
    
}

